package cn.micro.system.manage.service.impl;

import cn.micro.system.manage.common.security.utils.SecurityUtil;
import cn.micro.system.manage.entity.SysDimUser;
import cn.micro.system.manage.entity.dto.UpdatePasswordDTO;
import cn.micro.system.manage.entity.vo.RouterVo;
import cn.micro.system.manage.entity.vo.SysUserQueryVo;
import cn.micro.system.manage.mapper.SysDimUserMapper;
import cn.micro.system.manage.service.SysDimMenuService;
import cn.micro.system.manage.service.SysDimUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author wr
 * @since 2023-12-25
 */
@Service
public class SysDimUserServiceImpl extends ServiceImpl<SysDimUserMapper, SysDimUser> implements SysDimUserService {

    @Resource
    private SysDimMenuService sysDimMenuService;

    //用户列表
    @Override
    public List<SysDimUser> selectPage(int page,int size, SysUserQueryVo sysUserQueryVo) {
        PageHelper.startPage(page,size);
        return this.baseMapper.selectPage(sysUserQueryVo);
    }

    //更改用户状态
    @Override
    public void updateStatus(String id, Integer status) {
        //根据用户id查询
        SysDimUser sysUser = this.baseMapper.selectById(id);
        //设置修改状态
        sysUser.setStatus(status);
        //调用方法修改
        this.baseMapper.updateById(sysUser);
    }

    //username查询
    @Override
    public SysDimUser getUserInfoByUserName(String username) {
        QueryWrapper<SysDimUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        return baseMapper.selectOne(wrapper);
    }

    //根据用户名称获取用户信息（基本信息 和 菜单权限 和 按钮权限数据）
    @Override
    public Map<String, Object> getUserInfo(String username) {
        //根据username查询用户基本信息
        SysDimUser sysUser = this.getUserInfoByUserName(username);
        //根据userid查询菜单权限值
        List<RouterVo> routerVolist = sysDimMenuService.getUserMenuList(String.valueOf(sysUser.getId()));
        //根据userid查询按钮权限值
        List<String> permsList = sysDimMenuService.getUserButtonList(String.valueOf(sysUser.getId()));
        Map<String,Object> result = new HashMap<>();
        result.put("name",username);
        result.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        result.put("roles","[\"admin\"]");
        //菜单权限数据
        result.put("routers",routerVolist);
        //按钮权限数据
        result.put("buttons",permsList);
        return result;
    }

    @Override
    public List<SysDimUser> getByUsername(SysDimUser user) {
        LambdaQueryWrapper<SysDimUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(user.getUsername()),SysDimUser::getUsername,user.getUsername())
                .ne(StringUtils.isNotEmpty(user.getId()),SysDimUser::getId,user.getId());
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public SysDimUser getDetail(Long id) {
        return this.baseMapper.queryById(id);
    }

    @Override
    public boolean getOldPasswordIsTrue(UpdatePasswordDTO dto) {
        SysDimUser sysDimUser = this.baseMapper.selectById(dto.getId());
        if (SecurityUtil.matchesPassword(dto.getOldPassword(),sysDimUser.getPassword())){
            return true;
        }
        return false;
    }
}
